/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import _entity.CategoryInfo;
import _entity.ProductInfo;
import entity.Products;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author HP
 */
@Stateless
public class ProductsBean implements ProductsBeanRemote {

    @EJB
    private CategoriesBeanRemote categoriesBean;
    @PersistenceUnit(unitName = "_asmPU")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<ProductInfo> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Products.class));
        List<Products> list = getEntityManager().createQuery(cq).getResultList();
        List<ProductInfo> result = new ArrayList<ProductInfo>();

        for (Products c : list) {
            ProductInfo item = new ProductInfo();
            item.setDescription(c.getDescription());
            item.setId(c.getId());
            item.setName(c.getName());
            item.setPrice(c.getPrice());

            CategoryInfo cateInfo = new CategoryInfo();
            cateInfo.setProductsList(result);
            cateInfo.setId(c.getCategories().getId());
            cateInfo.setName(c.getCategories().getName());
            item.setCategory(cateInfo);

            result.add(item);
        }

        return result;
    }

    @Override
    public List<ProductInfo> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Products.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List<Products> list = q.getResultList();
        List<ProductInfo> result = new ArrayList<ProductInfo>();

        for (Products c : list) {
            ProductInfo item = new ProductInfo();
            item.setDescription(c.getDescription());
            item.setId(c.getId());
            item.setName(c.getName());
            item.setPrice(c.getPrice());

            CategoryInfo cateInfo = new CategoryInfo();
            cateInfo.setProductsList(result);
            cateInfo.setId(c.getCategories().getId());
            cateInfo.setName(c.getCategories().getName());
            item.setCategory(cateInfo);

            result.add(item);
        }

        return result;
    }

    @Override
    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<Products> rt = cq.from(Products.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public ProductInfo find(Object id) {
        Products c = getEntityManager().find(Products.class, id);
        if (c != null) {
            ProductInfo item = new ProductInfo();
            item.setDescription(c.getDescription());
            item.setId(c.getId());
            item.setName(c.getName());
            item.setPrice(c.getPrice());

            item.setCategory(categoriesBean.find(c.getCategories().getId()));

            return item;
        }
        return null;
    }

    @Override
    public void edit(ProductInfo product) {
        Products item = new Products();
        item.setDescription(product.getDescription());
        item.setId(product.getId());
        item.setName(product.getName());
        item.setPrice(product.getPrice());
        item.setCategories(categoriesBean.find_2(product.getCategory().getId()));

        getEntityManager().merge(item);
    }

    @Override
    public void create(ProductInfo product) {
       
        Products item = new Products();
        item.setDescription(product.getDescription());
        item.setId(product.getId());
        item.setName(product.getName());
        item.setPrice(product.getPrice());
        item.setCategories(categoriesBean.find_2(product.getCategory().getId()));

        getEntityManager().persist(item);
    }

    @Override
    public void remove(ProductInfo product) {
        Products item = new Products();
        item.setDescription(product.getDescription());
        item.setId(product.getId());
        item.setName(product.getName());
        item.setPrice(product.getPrice());
        item.setCategories(categoriesBean.find_2(product.getCategory().getId()));

        getEntityManager().remove(getEntityManager().merge(item));
    }
}
